package goclient

import (
	"fmt"
	"net/http"
)

func NewHeader() *Header {
	return &Header{header: &http.Header{}}
}

type Header struct {
	header *http.Header
}

func (h *Header) Get(key string) string {
	return h.header.Get(key)
}

func (h *Header) Set(key string, value string) {
	h.header.Set(key, value)
}

func (h *Header) Del(key string) {
	h.header.Del(key)
}

func (h *Header) Has(key string) bool {
	return h.header.Get(key) != ""
}

func (h *Header) CopyTo(header *Header) {
	h.copyTo(header.header)
}

func (h *Header) copyTo(header *http.Header) {
	myHeader := *h.header
	for key, _ := range myHeader {
		header.Set(key, myHeader.Get(key))
	}
}

func (h *Header) String() string {
	header := *h.header
	str := ""
	for key, _ := range header {
		str += fmt.Sprintf("%s: %s\r\n", key, header.Get(key))
	}
	return str
}

func (h *Header) Bytes() []byte {
	return []byte(h.String())
}

func (h *Header) Clone() *Header {
	headerCopy := h.header.Clone()
	return &Header{
		header: &headerCopy,
	}
}
